package usi.ch.onto.common;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.ListUtils;

public class StringUtils {

	@SuppressWarnings("unchecked")
	public static List<String> getIntersectionList(List<List<String>> listLists) {
		List<String> intersectionList = new ArrayList<String>();
		if (listLists != null && listLists.size() > 0) {
			intersectionList = listLists.get(0);
			if (listLists.size() > 1) {
				for (int i = 1; i < listLists.size(); i++)
					intersectionList = ListUtils.intersection(intersectionList,
							listLists.get(i));
			}
		}
		return intersectionList;
	}

	public static List<String> fastSplit(final String text, char separator1,
			char separator2) {
		final List<String> result = new ArrayList<String>();

		if (text != null && text.length() > 0) {
			int index1 = 0;
			int index2 = text.indexOf(separator1);
			int index3 = text.indexOf(separator2);
			while (index2 >= 0 || index3 >= 0) {
				if (index2 < index3 && index2 >= 0 || index3 < 0){
					result.add(text.substring(index1, index2));
					index1 = index2 + 1;
				} else {
					result.add(text.substring(index1, index3));
					index1 = index3 + 1;
				}
				index2 = text.indexOf(separator1, index1);
				index3 = text.indexOf(separator2, index1);
			}

			if (index1 < text.length() - 1) {
				result.add(text.substring(index1));
			}
		}

		return result;
	}
}
